package org.southyin.dao;

import org.southyin.model.CmsCategory;
import org.southyin.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CmsCategoryDaoImpl implements CmsCategoryDao {
    public List<CmsCategory> listCmsCategory() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<CmsCategory> list = new ArrayList<CmsCategory>();
        try {
            conn = DBUtils.getInstance().getConnection();
            String sql = "select * from cms_category where delMark = ?";
            ps = conn.prepareStatement(sql);
            ps.setBoolean(1, true);
            rs = ps.executeQuery();
            while (rs.next()) {
                CmsCategory category = new CmsCategory();
                category.setId(rs.getLong("id"));
                category.setCname(rs.getString("cname"));
                category.setParentId(rs.getLong("parentId"));
                category.setCreateTime(rs.getTimestamp("createTime"));
                category.setDelMark(rs.getBoolean("delMark"));
                list.add(category);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtils.getInstance().close(rs);
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
        return list;
    }
}
